//
//  KSelectDatePickerView.m
//  travelMerchant
//
//  Created by Jim huang on 2018/9/5.
//  Copyright © 2018年 Jim_LDS. All rights reserved.
//

#import "KSelectDatePickerView.h"

@interface KSelectDatePickerView (){
    sureBlock _sureBlock;

}

@property (nonatomic,strong) UIDatePicker * datePicker;

@property (nonatomic,strong) UIButton * sureBtn;

@property (nonatomic,strong) UIButton * cancelBtn;

@property (nonatomic,strong) NSDate * selectDate;

@property (nonatomic,strong) UILabel *titleLb;

@end

@implementation KSelectDatePickerView

//展示回调
+ (void)showSelectDatePickerWithSureBlock:(sureBlock)sureBlock{
    KSelectDatePickerView * view = [[KSelectDatePickerView alloc]initWithSureBlock:sureBlock];
    [view show];
}

- (instancetype)initWithSureBlock:(sureBlock)sureBlock
{
    self = [super init];
    if (self) {
        _sureBlock = sureBlock;
        [self setUI];
    }
    return self;
}

-(void)setUI{
    [super setUI];
    
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self);
        make.height.equalTo(@210);
    }];
    
    
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self.contentView);
        make.width.equalTo(@60);
        make.height.equalTo(@42);
    }];
    
//    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.equalTo(self.contentView);
//        make.width.equalTo(@110);
//        make.height.equalTo(@60);
//    }];
    
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sureBtn.mas_bottom);
        make.left.right.bottom.equalTo(self.contentView);
        
    }];
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.centerX.equalTo(self.contentView);
        make.width.equalTo(@110);
        make.height.equalTo(@22);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = RGBCOLOR(226, 226, 226);
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(42);
        make.left.right.equalTo(self.contentView);
        make.height.equalTo(@1);
    }];
    
    self.selectDate =[NSDate date];
    
}

#pragma mark ----action
-(void)dateChange:(UIDatePicker *)datePicker{
    self.selectDate = datePicker.date;
}

-(void)sureAction{
    
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    
    dateFormatter.dateFormat=@"yyyy-MM-dd";
    
    NSString * dateStr =[dateFormatter stringFromDate:self.selectDate];
    
    if (_sureBlock) {
        _sureBlock(dateStr);
    }
    [self hide];

    
}

#pragma mark ----getting&setting
-(UIDatePicker *)datePicker{
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc]init];
        [_datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
        [_datePicker setDatePickerMode:UIDatePickerModeDate];
        

        //默认为当天。
        
        [_datePicker setCalendar:[NSCalendar currentCalendar]];
        
        //    设置DatePicker的时区。
        
        [_datePicker setDate:[NSDate date]];

        
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        
        NSDate *currentDate = [NSDate date];
        
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        
        [comps setDay:0];//设置最大时间为：当前时间
        
        NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
        
        [_datePicker setMaximumDate:maxDate];
        [_datePicker addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:_datePicker];
    }
    return _datePicker;
}

-(UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:DMMainColor forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = DMFontSize(16);
        [_sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_sureBtn];
    }
    return _sureBtn;
}

-(UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:DMMainColor forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = DMFontSize(16);
        [_cancelBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_cancelBtn];
    }
    return _cancelBtn;
}

-(UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [UILabel new];
        _titleLb.text = @"选择收入日期";
        _titleLb.textColor = DMFontColor;
        _titleLb.font = DMFontSize(16);
        _titleLb.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_titleLb];
    }
    return _titleLb;
}


@end
